#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
const int N = 1e6 + 10;
int a[N], q[N], n, m, k;
void solve()
{
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    int hh = 0, tt = -1;
    for (int i = 0; i < n; i++)
    {
        // 判断队头是否滑出
        if (hh <= tt && i - k + 1 > q[hh])
        {
            hh++;
        }
        while (hh <= tt && a[q[tt]] >= a[i])
        {
            tt--;
        }
        q[++tt] = i;

        if (i >= k - 1)
            cout << a[q[hh]] << " ";
    }
    puts("");
    hh = 0, tt = -1;
    for (int i = 0; i < n; i++)
    {
        // 判断队头是否滑出
        if (hh <= tt && i - k + 1 > q[hh])
        {
            hh++;
        }
        while (hh <= tt && a[q[tt]] <= a[i])
        {
            tt--;
        }
        q[++tt] = i;

        if (i >= k - 1)
            cout << a[q[hh]] << " ";
    }
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
